//
//  YXPartnerHeaderView.m
//  CreditCard
//
//  Created by 杨旭 on 2017/12/23.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "YXPartnerHeaderView.h"
#import "YXSearchBarView.h"
#import "YXCustomSegmentedView.h"
#import "YXPartnerModel.h"
@interface YXPartnerHeaderView ()<UISearchBarDelegate>
/** 搜索框视图 */
@property (nonatomic ,strong) YXSearchBarView *searchBarView;

@property (nonatomic ,strong) UILabel *numberLab1;
@property (nonatomic ,strong) UILabel *numberLab2;
@property (nonatomic ,strong) UILabel *numberLab3;

@end

@implementation YXPartnerHeaderView

- (YXSearchBarView *)searchBarView {
    if(!_searchBarView) {
        _searchBarView = [[YXSearchBarView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 50)];
        _searchBarView.searchBar.delegate = self;
    }
    return _searchBarView;
}
- (UILabel *)numberLab1 {
    if (!_numberLab1) {
        _numberLab1 = [UILabel setLabelWithText:@"我的合伙人规模：0人" fontSize:12 color:color_textOne];
    }
    return _numberLab1;
}
- (UILabel *)numberLab2 {
    if (!_numberLab2) {
        _numberLab2 = [UILabel setLabelWithText:@"今日新增：0人" fontSize:12 color:color_textOne];
    }
    return _numberLab2;
}
- (UILabel *)numberLab3 {
    if (!_numberLab3) {
        _numberLab3 = [UILabel setLabelWithText:@"本月新增：0人" fontSize:12 color:color_textOne];
    }
    return _numberLab3;
}

- (void)setType:(NSString *)type {
    _type = type;
    [self setup];
}

- (void)setModel:(YXPartnerModel *)model {
    _model = model;
    _numberLab1.text = [NSString stringWithFormat:@"我的合伙人规模：%@人",_model.allCount];
    _numberLab2.text = [NSString stringWithFormat:@"今日新增：%@人",_model.dayCount];
    _numberLab3.text = [NSString stringWithFormat:@"本月新增：%@人",_model.monthConut];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        [self addSubview:self.searchBarView];
    }
    return self;
}

- (void)setup {
    
    if ([_type isEqualToString:@"合伙人列表"]) {
        [self addSubview:self.numberLab1];
        [self addSubview:self.numberLab2];
        [self addSubview:self.numberLab3];
        
    }else {
        YXCustomSegmentedView *headView = [[YXCustomSegmentedView alloc]initWithFrame:CGRectMake((kScreenWidth - 220)/2, 65, 220, 30)];
        headView.titleArray = @[@"月",@"日"];
        headView.selectedIndex = 0;//设置默认选择项索引
        headView.tint_Color = color_globalColor;
        [headView addTarget:self action:@selector(didclickHeadview:) forControlEvents:UIControlEventValueChanged];
        [self addSubview:headView];
        
    }
}

#pragma mark - Event
-(void)didclickHeadview:(YXCustomSegmentedView *)headview {
    
    NSLog(@"%s---%zd",__func__,headview.selectedIndex);
    if (self.selectTimeBlock) {
        self.selectTimeBlock(headview.selectedIndex);
    }
}

#pragma mark - UISearchBar Delegate
/**
 点击搜索框的代理方法
 
 @param searchBar 搜索按钮
 */
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [searchBar resignFirstResponder];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
//    NSLog(@"%@",searchText);
    if (self.selectNameBlock) {
        self.selectNameBlock(searchText);
    }
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [_searchBarView.searchBar resignFirstResponder];
}


- (void)layoutSubviews {
    [super layoutSubviews];
    YXWeakSelf
    [_numberLab1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.mas_left).offset(14);
        make.bottom.equalTo(weakSelf.mas_bottom).offset(-10);
        [_numberLab1 sizeToFit];
    }];
    
    [_numberLab2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.numberLab1.mas_right).offset(30);
        make.bottom.equalTo(weakSelf.mas_bottom).offset(-10);
        [_numberLab2 sizeToFit];
    }];
    
    [_numberLab3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.mas_right).offset(-14);
        make.bottom.equalTo(weakSelf.mas_bottom).offset(-10);
        [_numberLab3 sizeToFit];
    }];
}


@end
